// Copyright 2008 web2me.dev
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.googlecode.web2me.plugins;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Deploys a project jar into a particular directory
 * 
 * @goal deploy
 * 
 * @phase package
 * 
 * @author web2me
 */
public class DeployMojo extends AbstractMojo {

	/**
	 * The target directory where Maven places the compiled jar and classes.
     * @parameter expression="${deploy.targetDirectory}" default-value="target"
     * @required
	 */
	private File projectTargetDirectory;
	
	/**
	 * The directory where the jar is going to be placed
     * @parameter expression="${deploy.deployDirectory}" default-value="deploy"
     * @required
	 */
	private File deployDirectory;
	
	private final static Logger logger = Logger.getLogger(DeployMojo.class);
	
	public void execute() throws MojoExecutionException, MojoFailureException {
		String[] files = projectTargetDirectory.list(new JarFileFilter());
		
		logger.info("Path: " + projectTargetDirectory.getAbsolutePath());
		
		if(files == null || files.length == 0){
			throw new MojoFailureException("There are no jar files at the specified target directory");
		}
		
		logger.debug(files.length + " JAR file/s found");
		
		if(!deployDirectory.exists()){
			logger.debug("Creating deploy directory");
			deployDirectory.mkdirs();
		}
		
		File jarFile = new File(projectTargetDirectory + "/" + files[0]);
		File destinyFile = new File(deployDirectory.toString() + "/" + files[0]);
		
		logger.debug("Project JAR file: " + jarFile.toString());
		logger.debug("Deploy JAR file: " + destinyFile.toString());
		
		OutputStream os = null;
		InputStream is = null;
		
		try {
			is = new FileInputStream(jarFile);
			os = new FileOutputStream(destinyFile);
			
			byte[] buffer = new byte[1024];
			
			int k = 0;
			
			while((k = is.read(buffer)) != -1){
				os.write(buffer, 0, k);
			}
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException caught", e);
			throw new MojoExecutionException("FileNotFoundException caught", e);
		} catch (IOException e) {
			logger.error("IOException caught", e);
			throw new MojoExecutionException("IOException caught", e);
		}
		
		logger.info("JAR file was deployed to: " + destinyFile.toString());
	}

}
